package com.zmn.oms.admin.controller.work.modify;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.dto.work.modify.must.ZsReplyVisitDTO;
import com.zmn.oms.model.entity.conf.reply.ReplyAnswer;
import com.zmn.oms.model.entity.conf.reply.ReplyAnswerQuery;
import com.zmn.oms.model.entity.conf.reply.ReplyQuestion;
import com.zmn.oms.model.entity.conf.reply.ReplyQuestionQuery;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.modify.must.ZsReplyQuestionVO;
import com.zmn.oms.model.vo.work.modify.must.ZsReplyVisitVO;
import com.zmn.oms.services.interfaces.conf.reply.ReplyAnswerService;
import com.zmn.oms.services.interfaces.conf.reply.ReplyQuestionService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.customwork.ZsNormalCustomWorkBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类描述：工单列表主要操作controller
 *
 * @author lujia
 * @date 2018/11/07 16:32
 */
@Controller
@RequestMapping("/work/modify")
public class WorkModifyReplyController extends OmsBaseController {

    @Autowired
    ZsOrderWorkOperatePermissionBService zsOrderWorkOperatePermissionBService;
    @Autowired
    private ZsNormalCustomWorkBService zsNormalCustomWorkBService;
    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;

    @Autowired
    private ReplyQuestionService replyQuestionService;
    @Autowired
    private ReplyAnswerService replyAnswerService;

    /**
     * 回访
     */
    @RequestMapping(value = "/reply/visit")
    public ModelAndView replyVisit(Long orderId, Long workId, Long operateId) {
        ModelAndView mv = new ModelAndView("work/modify/replyvisit");
        ZsReplyVisitVO replyVisitVO = zsNormalCustomWorkBService.findReplyVisit(orderId, workId);

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);

        // 回访内容

        ReplyQuestionQuery replyQuestionQuery = new ReplyQuestionQuery();
        replyQuestionQuery.setBizType(orderWork.getBizType());
        replyQuestionQuery.setStatus(GlobalConsts.YES);
        List<ReplyQuestion> replyQuestionList = replyQuestionService.listByQuery(replyQuestionQuery);

        List<ZsReplyQuestionVO> replyQuestionVOList = Lists.newArrayList();
        // 遍历查询出的问题集合
        replyQuestionList.forEach(e -> {
            ReplyAnswerQuery replyAnswerQuery = new ReplyAnswerQuery();
            List<Integer> ids = Arrays.stream(e.getAnswerIdStr().split("\\$")).filter(id -> !id.isEmpty()).map(Integer::parseInt).collect(Collectors.toList());
            // 根据每个答案id查询对应答案集合
            replyAnswerQuery.setAnswerIdList(ids);
            replyAnswerQuery.setStatus(GlobalConsts.YES);
            List<ReplyAnswer> replyAnswerList = replyAnswerService.listByQuery(replyAnswerQuery);
            List<KVDict<Integer>> answers = Lists.newArrayList();
            replyAnswerList.forEach(item -> {
                answers.add(KVDict.<Integer>builder().value(item.getAnswerId()).text(item.getAnswer()).build());
            });
            ZsReplyQuestionVO zsReplyQuestionVO = ZsReplyQuestionVO.builder().answer(answers).type(e.getAnswerType()).question(e.getQuestion()).questionId(e.getQuestionId())
                    .items(Arrays.stream(e.getAnswerCategStr().split("\\$")).filter(id -> !id.isEmpty()).map(Integer::parseInt).collect(Collectors.toList())).build();
            replyQuestionVOList.add(zsReplyQuestionVO);
        });

        replyVisitVO.setReplyItemList(super.getVisitCategList());
        replyVisitVO.setReplyVisitList(replyQuestionVOList);
        mv.addObject("replyVisitVO", replyVisitVO);
        mv.addObject("operateId", operateId);
        return mv;
    }


    /**
     * 保存回访
     */
    @RequestMapping(value = "/save/reply/visit")
    @ResponseBody
    public ResultDTO saveReplyVisit(@RequestBody ZsReplyVisitDTO replyVisitDTO) throws Exception {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(replyVisitDTO.getOrderId(), replyVisitDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_REPLY_VISIT);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        zsNormalCustomWorkBService.saveReplyVisit(replyVisitDTO);
        return ResultDTO.success();
    }
}
